package com.fand.platform.authority.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dao.UserGrDao;
import com.fand.platform.authority.dto.UserGrDto;
import com.fand.platform.authority.dto.UserGrBuArDto;
import com.fand.platform.authority.dto.UserGrRoDto;
import com.fand.platform.authority.dto.UserGrUsDto;

/**
 * 用户组服务
 *
 * @author yaoHT
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class UserGrDefaultService implements UserGrService {
	private UserGrDao userGrDao;
	
	@Override
	@CacheEvict(value = UserGrService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	@Transactional
	public UserGrDto saveUserGrByIdOrCodeForUserGr(UserGrDto saveUserGr) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGr.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrDto oldUserGr = this.userGrDao.selectUserGrBy1ForMappedObject(UserGrDto.class,
				saveUserGr, null, whereEqProperties);

		if (oldUserGr == null) {
			this.userGrDao.insertUserGrBy1(saveUserGr);

			return saveUserGr;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGr, oldUserGr, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrBy1(saveUserGr, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGr, oldUserGr, updateProperties);
		}

		return oldUserGr;
	}

	@Override
	@Transactional
	public UserGrDto saveUserGrBy2ForUserGr(UserGrDto saveUserGr) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGr.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrDto oldUserGr = this.userGrDao.selectUserGrBy1ForMappedObject(UserGrDto.class,
				saveUserGr, null, whereEqProperties);

		if (oldUserGr == null) {
			this.userGrDao.insertUserGrBy1(saveUserGr, new String[] {});

			return saveUserGr;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGr, oldUserGr, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrBy1(saveUserGr, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGr, oldUserGr, updateProperties);
		}

		return oldUserGr;
	}

	@Override
	@Transactional
	public int deleteUserGrBy1(UserGrDto deleteUserGr) {
		return this.userGrDao.deleteUserGrBy1(deleteUserGr, new String[] {});
	}

	@Override
	@Transactional
	public int deleteUserGrByIds(UserGrDto deleteUserGr) {
		return this.userGrDao.deleteUserGrByIds(deleteUserGr);
	}

	@Override
	@Transactional
	public int setUserGrBy1(UserGrDto setUserGr) {
		return this.userGrDao.updateUserGrBy1(setUserGr, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setUserGrBy2(UserGrDto setUserGr) {
		return this.userGrDao.updateUserGrBy2(setUserGr);
	}

	@Override
	public Integer readUserGrByCodeNqIdForInteger(UserGrDto readUserGr) {
		return this.userGrDao.selectUserGrByCodeNqIdForInteger(readUserGr);
	}

	@Override
	public Integer readUserGrBy2ForInteger(UserGrDto readUserGr) {
		return this.userGrDao.selectUserGrBy2ForObject(Integer.class, readUserGr, null);
	}

	@Override
	public List<Integer> readListUserGrBy1(UserGrDto readUserGr) {
		return this.userGrDao.selectUserGrBy1ForObjectList(Integer.class, readUserGr, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListUserGrBy2(UserGrDto readUserGr) {
		return this.userGrDao.selectUserGrBy2ForObjectList(Integer.class, readUserGr, null);
	}

	@Override
	public UserGrDto getUserGrBy1(UserGrDto getUserGr) {
		return this.userGrDao.selectUserGrBy1ForMappedObject(UserGrDto.class, getUserGr, null, "id");
	}

	@Override
	public UserGrDto getUserGrById(UserGrDto getUserGr) {
		return this.userGrDao.selectUserGrBy1ForMappedObject(UserGrDto.class, getUserGr, null, "id");
	}

	@Override
	public List<UserGrDto> findUserGrBy1(UserGrDto findUserGr) {
		return this.userGrDao.selectUserGrBy1ForMappedObjectList(UserGrDto.class, findUserGr,
				new String[] {}, new String[] {});
	}

	@Override
	public List<UserGrDto> findUserGrBy2(UserGrDto findUserGr) {
		return this.userGrDao.selectUserGrBy2ForMappedObjectList(UserGrDto.class, findUserGr);
	}

	@Override
	public PaginationDto<UserGrDto> paginationUserGrBy1ForUserGrList(UserGrDto paginationUserGr) {
		return this.userGrDao.paginationUserGrBy1ForMappedObjectList(UserGrDto.class,
				paginationUserGr);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationUserGrBy1ForMapList(UserGrDto paginationUserGr) {
		return this.userGrDao.paginationUserGrBy1ForMapList(paginationUserGr);
	}

	@Override
	@Transactional
	public UserGrBuArDto saveUserGrBuArByIdOrCodeForUserGrBuAr(UserGrBuArDto saveUserGrBuAr) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGrBuAr.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrBuArDto oldUserGrBuAr = this.userGrDao.selectUserGrBuArBy1ForMappedObject(UserGrBuArDto.class,
				saveUserGrBuAr, null, whereEqProperties);

		if (oldUserGrBuAr == null) {
			this.userGrDao.insertUserGrBuArBy1(saveUserGrBuAr);

			return saveUserGrBuAr;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGrBuAr, oldUserGrBuAr, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrBuArBy1(saveUserGrBuAr, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGrBuAr, oldUserGrBuAr, updateProperties);
		}

		return oldUserGrBuAr;
	}

	@Override
	@Transactional
	public UserGrBuArDto saveUserGrBuArBy2ForUserGrBuAr(UserGrBuArDto saveUserGrBuAr) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGrBuAr.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrBuArDto oldUserGrBuAr = this.userGrDao.selectUserGrBuArBy1ForMappedObject(UserGrBuArDto.class,
				saveUserGrBuAr, null, whereEqProperties);

		if (oldUserGrBuAr == null) {
			this.userGrDao.insertUserGrBuArBy1(saveUserGrBuAr, new String[] {});

			return saveUserGrBuAr;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGrBuAr, oldUserGrBuAr, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrBuArBy1(saveUserGrBuAr, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGrBuAr, oldUserGrBuAr, updateProperties);
		}

		return oldUserGrBuAr;
	}

	@Override
	@Transactional
	public int deleteUserGrBuArBy1(UserGrBuArDto deleteUserGrBuAr) {
		return this.userGrDao.deleteUserGrBuArBy1(deleteUserGrBuAr, new String[] {});
	}

	@Override
	@Transactional
	public int deleteUserGrBuArByIds(UserGrBuArDto deleteUserGrBuAr) {
		return this.userGrDao.deleteUserGrBuArByIds(deleteUserGrBuAr);
	}

	@Override
	@Transactional
	public int setUserGrBuArBy1(UserGrBuArDto setUserGrBuAr) {
		return this.userGrDao.updateUserGrBuArBy1(setUserGrBuAr, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setUserGrBuArBy2(UserGrBuArDto setUserGrBuAr) {
		return this.userGrDao.updateUserGrBuArBy2(setUserGrBuAr);
	}

	@Override
	public Integer readUserGrBuArByCodeNqIdForInteger(UserGrBuArDto readUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArByCodeNqIdForInteger(readUserGrBuAr);
	}

	@Override
	public Integer readUserGrBuArBy2ForInteger(UserGrBuArDto readUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy2ForObject(Integer.class, readUserGrBuAr, null);
	}

	@Override
	public List<Integer> readListUserGrBuArBy1(UserGrBuArDto readUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy1ForObjectList(Integer.class, readUserGrBuAr, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListUserGrBuArBy2(UserGrBuArDto readUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy2ForObjectList(Integer.class, readUserGrBuAr, null);
	}

	@Override
	public UserGrBuArDto getUserGrBuArBy1(UserGrBuArDto getUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy1ForMappedObject(UserGrBuArDto.class, getUserGrBuAr, null, "id");
	}

	@Override
	public UserGrBuArDto getUserGrBuArById(UserGrBuArDto getUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy1ForMappedObject(UserGrBuArDto.class, getUserGrBuAr, null, "id");
	}

	@Override
	public List<UserGrBuArDto> findUserGrBuArBy1(UserGrBuArDto findUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy1ForMappedObjectList(UserGrBuArDto.class, findUserGrBuAr,
				new String[] {}, new String[] {});
	}

	@Override
	public List<UserGrBuArDto> findUserGrBuArBy2(UserGrBuArDto findUserGrBuAr) {
		return this.userGrDao.selectUserGrBuArBy2ForMappedObjectList(UserGrBuArDto.class, findUserGrBuAr);
	}

	@Override
	public PaginationDto<UserGrBuArDto> paginationUserGrBuArBy1ForUserGrBuArList(UserGrBuArDto paginationUserGrBuAr) {
		return this.userGrDao.paginationUserGrBuArBy1ForMappedObjectList(UserGrBuArDto.class,
				paginationUserGrBuAr);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationUserGrBuArBy1ForMapList(UserGrBuArDto paginationUserGrBuAr) {
		return this.userGrDao.paginationUserGrBuArBy1ForMapList(paginationUserGrBuAr);
	}

	@Override
	@Transactional
	public UserGrRoDto saveUserGrRoByIdOrCodeForUserGrRo(UserGrRoDto saveUserGrRo) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGrRo.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrRoDto oldUserGrRo = this.userGrDao.selectUserGrRoBy1ForMappedObject(UserGrRoDto.class,
				saveUserGrRo, null, whereEqProperties);

		if (oldUserGrRo == null) {
			this.userGrDao.insertUserGrRoBy1(saveUserGrRo);

			return saveUserGrRo;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGrRo, oldUserGrRo, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrRoBy1(saveUserGrRo, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGrRo, oldUserGrRo, updateProperties);
		}

		return oldUserGrRo;
	}

	@Override
	@Transactional
	public UserGrRoDto saveUserGrRoBy2ForUserGrRo(UserGrRoDto saveUserGrRo) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGrRo.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrRoDto oldUserGrRo = this.userGrDao.selectUserGrRoBy1ForMappedObject(UserGrRoDto.class,
				saveUserGrRo, null, whereEqProperties);

		if (oldUserGrRo == null) {
			this.userGrDao.insertUserGrRoBy1(saveUserGrRo, new String[] {});

			return saveUserGrRo;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGrRo, oldUserGrRo, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrRoBy1(saveUserGrRo, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGrRo, oldUserGrRo, updateProperties);
		}

		return oldUserGrRo;
	}

	@Override
	@Transactional
	public int deleteUserGrRoBy1(UserGrRoDto deleteUserGrRo) {
		return this.userGrDao.deleteUserGrRoBy1(deleteUserGrRo, new String[] {});
	}

	@Override
	@Transactional
	public int deleteUserGrRoByIds(UserGrRoDto deleteUserGrRo) {
		return this.userGrDao.deleteUserGrRoByIds(deleteUserGrRo);
	}

	@Override
	@Transactional
	public int setUserGrRoBy1(UserGrRoDto setUserGrRo) {
		return this.userGrDao.updateUserGrRoBy1(setUserGrRo, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setUserGrRoBy2(UserGrRoDto setUserGrRo) {
		return this.userGrDao.updateUserGrRoBy2(setUserGrRo);
	}

	@Override
	public Integer readUserGrRoByCodeNqIdForInteger(UserGrRoDto readUserGrRo) {
		return this.userGrDao.selectUserGrRoByCodeNqIdForInteger(readUserGrRo);
	}

	@Override
	public Integer readUserGrRoBy2ForInteger(UserGrRoDto readUserGrRo) {
		return this.userGrDao.selectUserGrRoBy2ForObject(Integer.class, readUserGrRo, null);
	}

	@Override
	public List<Integer> readListUserGrRoBy1(UserGrRoDto readUserGrRo) {
		return this.userGrDao.selectUserGrRoBy1ForObjectList(Integer.class, readUserGrRo, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListUserGrRoBy2(UserGrRoDto readUserGrRo) {
		return this.userGrDao.selectUserGrRoBy2ForObjectList(Integer.class, readUserGrRo, null);
	}

	@Override
	public UserGrRoDto getUserGrRoBy1(UserGrRoDto getUserGrRo) {
		return this.userGrDao.selectUserGrRoBy1ForMappedObject(UserGrRoDto.class, getUserGrRo, null, "id");
	}

	@Override
	public UserGrRoDto getUserGrRoById(UserGrRoDto getUserGrRo) {
		return this.userGrDao.selectUserGrRoBy1ForMappedObject(UserGrRoDto.class, getUserGrRo, null, "id");
	}

	@Override
	public List<UserGrRoDto> findUserGrRoBy1(UserGrRoDto findUserGrRo) {
		return this.userGrDao.selectUserGrRoBy1ForMappedObjectList(UserGrRoDto.class, findUserGrRo,
				new String[] {}, new String[] {});
	}

	@Override
	public List<UserGrRoDto> findUserGrRoBy2(UserGrRoDto findUserGrRo) {
		return this.userGrDao.selectUserGrRoBy2ForMappedObjectList(UserGrRoDto.class, findUserGrRo);
	}

	@Override
	public PaginationDto<UserGrRoDto> paginationUserGrRoBy1ForUserGrRoList(UserGrRoDto paginationUserGrRo) {
		return this.userGrDao.paginationUserGrRoBy1ForMappedObjectList(UserGrRoDto.class,
				paginationUserGrRo);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationUserGrRoBy1ForMapList(UserGrRoDto paginationUserGrRo) {
		return this.userGrDao.paginationUserGrRoBy1ForMapList(paginationUserGrRo);
	}

	@Override
	@Transactional
	public UserGrUsDto saveUserGrUsByIdOrCodeForUserGrUs(UserGrUsDto saveUserGrUs) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGrUs.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrUsDto oldUserGrUs = this.userGrDao.selectUserGrUsBy1ForMappedObject(UserGrUsDto.class,
				saveUserGrUs, null, whereEqProperties);

		if (oldUserGrUs == null) {
			this.userGrDao.insertUserGrUsBy1(saveUserGrUs);

			return saveUserGrUs;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGrUs, oldUserGrUs, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrUsBy1(saveUserGrUs, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGrUs, oldUserGrUs, updateProperties);
		}

		return oldUserGrUs;
	}

	@Override
	@Transactional
	public UserGrUsDto saveUserGrUsBy2ForUserGrUs(UserGrUsDto saveUserGrUs) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveUserGrUs.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		UserGrUsDto oldUserGrUs = this.userGrDao.selectUserGrUsBy1ForMappedObject(UserGrUsDto.class,
				saveUserGrUs, null, whereEqProperties);

		if (oldUserGrUs == null) {
			this.userGrDao.insertUserGrUsBy1(saveUserGrUs, new String[] {});

			return saveUserGrUs;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveUserGrUs, oldUserGrUs, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.userGrDao.updateUserGrUsBy1(saveUserGrUs, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveUserGrUs, oldUserGrUs, updateProperties);
		}

		return oldUserGrUs;
	}

	@Override
	@Transactional
	public int deleteUserGrUsBy1(UserGrUsDto deleteUserGrUs) {
		return this.userGrDao.deleteUserGrUsBy1(deleteUserGrUs, new String[] {});
	}

	@Override
	@Transactional
	public int deleteUserGrUsByIds(UserGrUsDto deleteUserGrUs) {
		return this.userGrDao.deleteUserGrUsByIds(deleteUserGrUs);
	}

	@Override
	@Transactional
	public int setUserGrUsBy1(UserGrUsDto setUserGrUs) {
		return this.userGrDao.updateUserGrUsBy1(setUserGrUs, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setUserGrUsBy2(UserGrUsDto setUserGrUs) {
		return this.userGrDao.updateUserGrUsBy2(setUserGrUs);
	}

	@Override
	public Integer readUserGrUsByCodeNqIdForInteger(UserGrUsDto readUserGrUs) {
		return this.userGrDao.selectUserGrUsByCodeNqIdForInteger(readUserGrUs);
	}

	@Override
	public Integer readUserGrUsBy2ForInteger(UserGrUsDto readUserGrUs) {
		return this.userGrDao.selectUserGrUsBy2ForObject(Integer.class, readUserGrUs, null);
	}

	@Override
	public List<Integer> readListUserGrUsBy1(UserGrUsDto readUserGrUs) {
		return this.userGrDao.selectUserGrUsBy1ForObjectList(Integer.class, readUserGrUs, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListUserGrUsBy2(UserGrUsDto readUserGrUs) {
		return this.userGrDao.selectUserGrUsBy2ForObjectList(Integer.class, readUserGrUs, null);
	}

	@Override
	public UserGrUsDto getUserGrUsBy1(UserGrUsDto getUserGrUs) {
		return this.userGrDao.selectUserGrUsBy1ForMappedObject(UserGrUsDto.class, getUserGrUs, null, "id");
	}

	@Override
	public UserGrUsDto getUserGrUsById(UserGrUsDto getUserGrUs) {
		return this.userGrDao.selectUserGrUsBy1ForMappedObject(UserGrUsDto.class, getUserGrUs, null, "id");
	}

	@Override
	public List<UserGrUsDto> findUserGrUsBy1(UserGrUsDto findUserGrUs) {
		return this.userGrDao.selectUserGrUsBy1ForMappedObjectList(UserGrUsDto.class, findUserGrUs,
				new String[] {}, new String[] {});
	}

	@Override
	public List<UserGrUsDto> findUserGrUsBy2(UserGrUsDto findUserGrUs) {
		return this.userGrDao.selectUserGrUsBy2ForMappedObjectList(UserGrUsDto.class, findUserGrUs);
	}

	@Override
	public PaginationDto<UserGrUsDto> paginationUserGrUsBy1ForUserGrUsList(UserGrUsDto paginationUserGrUs) {
		return this.userGrDao.paginationUserGrUsBy1ForMappedObjectList(UserGrUsDto.class,
				paginationUserGrUs);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationUserGrUsBy1ForMapList(UserGrUsDto paginationUserGrUs) {
		return this.userGrDao.paginationUserGrUsBy1ForMapList(paginationUserGrUs);
	}
}